/*
 * Copyright 2022-2023 the original author(https://github.com/wj596)
 * 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */
package org.jsets.fastboot.flowable.controller;

import org.flowable.engine.history.HistoricProcessInstance;
import org.jsets.fastboot.model.ListResp;
import org.jsets.fastboot.flowable.model.dto.InstanceQuery;
import org.jsets.fastboot.flowable.service.IHistoricService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author wangjie (https://github.com/wj596)
 * @date 2021.06.25 11:23
 */
@Api(tags = "流程实例")
@RestController
@RequestMapping("/flow/historics")
public class HistoricController {

	@Autowired
	private IHistoricService service;

	@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("查询历史流程实例")
	@ApiImplicitParams({
		 @ApiImplicitParam(name="page_no", value="当前页码", required=true, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="page_size", value="每页条数", required=true, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="key", value="KEY", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="name", value="名称", required=false, dataType = "String", dataTypeClass = String.class,  paramType="query")
		,@ApiImplicitParam(name="business_key", value="业务KEY", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
	})
	public ListResp<HistoricProcessInstance> selectPage(
			@RequestParam(name = "page_no", required = true) Integer pageNo,
			@RequestParam(name = "page_size", required = true) Integer pageSize,
			@RequestParam(name = "key", required = false) String key,
			@RequestParam(name = "name", required = false) String name,
			@RequestParam(name = "business_key", required = false) String businessKey) {
		InstanceQuery criteria = new InstanceQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setDefinitionKey(key)
				.setDefinitionName(name)
				.setBusinessKey(businessKey);
		Page<HistoricProcessInstance> result = this.service.selectHistoricProcessInstancePage(criteria);
		ListResp<HistoricProcessInstance> respond = ListResp.ok();
		respond.setData(result.getRecords());
		respond.setTotalCount(result.getTotal());
		return respond;
	}

}